Teamwork in Git


Git Remote

When you work in a group, it is typical to collaborate with others using a Git server, or a website like Github (highly recommended).

A remote is essentially a shared Git repository that allows for multiple collaborators. Now keep in mind collaborators work independently, and merge their changes when ready.

Cloning a Remote Repository

Now how do you get to work on your partner's git repository...on let's say your organization's github? Well why not clone the repository?

You can use the git clone command.

It works locally:

$ ls
git-repository

$ git clone git-project/ git-clone-project # git clone remote-repo output-directory(optional)
Cloning into 'git-clone-project'...
done. 

$ ls
git-clone-project git-project

It works on remote sites, such as Github:

# HTTP Clone
$ git clone https://github.com/user/repo.git

# SSH Clone
$ git clone git@github.com:user/repo.git

Fetching Changes

The command git fetch downloads objects and changes from a remote repository origin.

It however doesn't automatically merge these changes to your current branch, but instead keeps all the changes in a new branch origin/branch-name.

Now let's see this in action:

$ git branch
* master

$ git fetch
remote: Counting objects: 3, done. 
remote: Compressing objects: 100% (3/3), done. 
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done. 
From /home/user/git-project 
* [new branch]      master     ->  origin/master

$ git branch
* master
  remotes/origin/master

To merge these changes, it's like last section, use the command git merge in the branch you want to have merge with the target branch.

To merge the fetch changes for our example, we do git merge origin/master on branch master.

Pushing Changes

In Git, the git push origin branch-name command pushes the branch branch-name, and all of the committed changes, to the remote origin. This branch can now be reviewed and fetched by collaborators.

Looking at the Remotes

In Git, the git-remote -v command returns a list of remote repositories that the current project is connected to.

  • Git lists the name of the remote repository as well as its locations.
  • Git automatically names this remote origin, because it refers to the remote repository of origin.
    • However, it is possible to safely change its name.
  • The remote is listed twice: once for (fetch) and once for (push).
$ git-remote -v 
origin  /home/user/git-project/
(fetch)
origin  /home/user/git-project/
(push)

A Typical Collaboration Workflow

A typical collaboration workflow is:

  1. Fetch and merge changes from the remote
  2. Create a branch to work on a new project feature
  3. Develop the feature on a branch and commit the work
  4. Fetch and merge from the remote again (in case new commits were made)
  5. Push branch up to the remote for review

Note: Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command.

Here's a bash script that can be made to ease the need of many commands:

#!/bin/bash
git fetch
git merge origin/master 

echo "New Branch Name: "
read branch

git branch $branch
git checkout $branch

git fetch
git add * 

echo "Message: "
read message

git commit -m $message

git merge origin/master

git push -u origin $branch